package com.qf.medicaldoctoradmin.healthMonitor.drugRemind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qf.common.entity.DrugSchedule;
import com.qf.common.utils.UserUtils;
import com.qf.medicaldoctoradmin.healthMonitor.drugRemind.dao.DrugScheduleDao;
import com.qf.medicaldoctoradmin.healthMonitor.drugRemind.service.DrugScheduleService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class DrugScheduleServiceImpl implements DrugScheduleService {
   @Resource
   private DrugScheduleDao drugScheduleDao;
    @Override
    public List<DrugSchedule> getList() {
        Integer uid = UserUtils.getUid();
        List<DrugSchedule> scheduleList = drugScheduleDao.selectList(new LambdaQueryWrapper<DrugSchedule>()
                .eq(DrugSchedule::getUserId, uid));
        return scheduleList;
    }

    @Override
    public List<DrugSchedule> getEnableList() {
        List<DrugSchedule> scheduleList = drugScheduleDao.selectList(new LambdaQueryWrapper<DrugSchedule>()
                .eq(DrugSchedule::getUserId, UserUtils.getUid())
                .eq(DrugSchedule::getIsEnabled, 1));
        return scheduleList;
    }

    @Override
    public void insert(DrugSchedule drugSchedule) {
        if (drugSchedule.getRemindTime() == null) {
            throw new RuntimeException("请先输入完整信息再开启用药提醒");
        }
        drugSchedule.setUserId(UserUtils.getUid());
        drugScheduleDao.insert(drugSchedule);
    }

    @Override
    public void update(DrugSchedule drugSchedule) {
        if (drugSchedule.getRemindTime() == null) {
            throw new RuntimeException("请先输入完整信息再更新用药提醒");
        }
        drugScheduleDao.update(drugSchedule,
                new LambdaUpdateWrapper<DrugSchedule>()
                        .set(DrugSchedule::getIsEnabled, 0)
                        .set(DrugSchedule::getUserId, UserUtils.getUid())
                        .eq(DrugSchedule::getId, drugSchedule.getId())
        );
    }

    @Override
    public void delete(Integer id) {
        if (id == null) {
            throw new RuntimeException("请先选择具体用药提醒再删除");
        }
        drugScheduleDao.deleteById(id);
    }

}
